home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / c / pro22 / cbinsert.c < prev    next >
Text File  |  1990-06-20  |  4KB  |  161 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbinsert.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <stdlib.h>*/
  10. /*#include <string.h>*/
  11.  
  12. /* library headers */
  13. #include <blkio.h>
  14. #include <lseq.h>
  15.  
  16. /* local headers */
  17. #include "cbase_.h"
  18.  
  19. /*man---------------------------------------------------------------------------
  20. NAME
  21.      cbinsert - insert record
  22.  
  23. SYNOPSIS
  24.      #include <cbase.h>
  25.  
  26.      int cbinsert(cbp, buf)
  27.      cbase_t *cbp;
  28.      const void *buf;
  29.  
  30. DESCRIPTION
  31.      The cbinsert function inserts the record pointed to by buf into
  32.      cbase cbp.  The record is inserted after the current record.  The
  33.      record cursor and all the key cursors are set to the inserted
  34.      record.
  35.  
  36.      cbinsert will fail if one or more of the following is true:
  37.  
  38.      [EINVAL]       cbp is not a valid cbase pointer.
  39.      [EINVAL]       buf is the NULL pointer.
  40.      [CBEDUP]       A field in the record pointed to by buf contains
  41.                     an illegal duplicate key.
  42.      [CBELOCK]      cbp is not write locked.
  43.      [CBENOPEN]     cbp is not open.
  44.  
  45. SEE ALSO
  46.      cbdelcur, cbrcursor.
  47.  
  48. DIAGNOSTICS
  49.      Upon successful completion, a value of 0 is returned.  Otherwise,
  50.      a value of -1 is returned, and errno set to indicate the error.
  51.  
  52. ------------------------------------------------------------------------------*/
  53. int cbinsert(cbp, buf)
  54. cbase_t *cbp;
  55. const void *buf;
  56. {
  57.     void *buf2 = NULL;
  58.     cbrpos_t cbrpos = NIL;
  59.     lspos_t lspos = NIL;
  60.     int i = 0;
  61.     int found = 0;
  62.  
  63.     /* validate arguments */
  64.     if (!cb_valid(cbp) || buf == NULL) {
  65.         errno = EINVAL;
  66.         return -1;
  67.     }
  68.  
  69.     /* check if not open */
  70.     if (!(cbp->flags & CBOPEN)) {
  71.         errno = CBENOPEN;
  72.         return -1;
  73.     }
  74.  
  75.     /* check if not write locked */
  76.     if (!(cbp->flags & CBWRLCK)) {
  77.         errno = CBELOCK;
  78.         return -1;
  79.     }
  80.  
  81.     /* check for illegal duplicate keys */
  82.     if (cbgetrcur(cbp, &cbrpos) == -1) {    /* save current record pos */
  83.         CBEPRINT;
  84.         return -1;
  85.     }
  86.     for (i = 0; i < cbp->fldc; ++i) {
  87.         if (cbp->fldv[i].flags & CB_FKEY) {
  88.             if (cbp->fldv[i].flags & CB_FUNIQ) {
  89.                 buf2 = calloc((size_t)1, cbp->fldv[i].len);
  90.                 if (buf2 == NULL) {
  91.                     CBEPRINT;
  92.                     errno = ENOMEM;
  93.                     return -1;
  94.                 }
  95.                 memcpy(buf2, ((char *)buf + cbp->fldv[i].offset), cbp->fldv[i].len);
  96.                 found = cbkeysrch(cbp, i, buf2);
  97.                 if (found == -1) {
  98.                     CBEPRINT;
  99.                     free(buf2);
  100.                     return -1;
  101.                 }
  102.                 if (found == 1) {
  103.                     free(buf2);
  104.                     errno = CBEDUP;
  105.                     return -1;
  106.                 }
  107.                 free(buf2);
  108.                 buf2 = NULL;
  109.             }
  110.         }
  111.     }
  112.     if (cbsetrcur(cbp, &cbrpos) == -1) {    /* restore record pos */
  113.         CBEPRINT;
  114.         return -1;
  115.     }
  116.  
  117.     /* insert record */
  118.     if (lsinsert(cbp->lsp, buf) == -1) {
  119.         CBEPRINT;
  120.         return -1;
  121.     }
  122.  
  123.     /* get position of inserted record */
  124.     if (lsgetcur(cbp->lsp, &lspos) == -1) {
  125.         CBEPRINT;
  126.         return -1;
  127.     }
  128.     cbrpos = lspos;
  129.  
  130.     /* insert keys */
  131.     for (i = 0; i < cbp->fldc; ++i) {
  132.         if (cbp->fldv[i].flags & CB_FKEY) {
  133.             /* construct (key, record postion) pair */
  134.             if (btkeysize(cbp->btpv[i]) != (cbp->fldv[i].len + sizeof(cbrpos_t))) {
  135.                 CBEPRINT;
  136.                 errno = CBEPANIC;
  137.                 return -1;
  138.             }
  139.             buf2 = calloc((size_t)1, cbp->fldv[i].len + sizeof(cbrpos_t));
  140.             if (buf2 == NULL) {
  141.                 CBEPRINT;
  142.                 errno = ENOMEM;
  143.                 return -1;
  144.             }
  145.             memcpy(buf2, ((char *)buf + cbp->fldv[i].offset), cbp->fldv[i].len);
  146.             memcpy(((char *)buf2 + cbp->fldv[i].len), &cbrpos, sizeof(cbrpos_t));
  147.             /* insert key */
  148.             if (btinsert(cbp->btpv[i], buf2) == -1) {
  149.                 CBEPRINT;
  150.                 free(buf2);
  151.                 return -1;
  152.             }
  153.             free(buf2);
  154.             buf2 = NULL;
  155.         }
  156.     }
  157.  
  158.     errno = 0;
  159.     return 0;
  160. }
  161.